package com.talkweb.newproject.admin.rest;

import com.talkweb.newproject.admin.sysresource.dto.SysResourceDTO;
import com.talkweb.newproject.admin.sysresource.service.SysResourceService;
import com.talkweb.newproject.api.model.admin.SysResourceParam;
import com.talkweb.newproject.api.model.common.Response;
import com.talkweb.newproject.api.model.common.ResponseResult;
import com.talkweb.newproject.api.model.common.Status;
import com.talkweb.newproject.api.model.shiro.RolePermission;
import com.talkweb.newproject.api.model.shiro.RolePermissionParam;
import com.talkweb.newproject.common.utils.MapperUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import javax.xml.crypto.dsig.spec.ExcC14NParameterSpec;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by qsf on 2019/4/23.
 */
@RestController
@RequestMapping(value="/api/sysResource")
@Slf4j
public class SysResourceRest {
   @Autowired
    private SysResourceService sysResourceService;

   @Autowired
    MapperUtil mapperUtil;

    /**
     * 根据id查询resource信息
     * @param id
     * @return
     * @throws Exception
     */
   @ResponseBody
   @RequestMapping(value="/get/v1/{id}",method = RequestMethod.POST)
   public ResponseResult<SysResourceDTO> querySysResourceByPrimaryKey(@PathVariable("id") String id) throws Exception {
       ResponseResult<SysResourceDTO> result = new ResponseResult<>();
       try{
           SysResourceDTO sysResourceDTO = sysResourceService.querySysResourceByPrimaryKey(id);
           result.setStatus(Status.SUCCESS);
           result.setData(sysResourceDTO);
           result.setMessage("查询成功");
           return result;
       }catch(Exception e) {
           log.error(e.getMessage());
           result.setStatus(Status.FAILD);
           result.setMessage("执行异常,请重试");
           return result;
       }

   }


    /**
     * 获取list对象
     * @param obj
     * @return
     * @throws Exception
     */
   @ResponseBody
   @RequestMapping(value="/getSysResourceList",method = RequestMethod.POST)
   public ResponseResult<List<SysResourceDTO>> querySysResourceList(@RequestBody SysResourceParam obj)  throws Exception {
       ResponseResult<List<SysResourceDTO>> result = new ResponseResult<>();
       try{
           List<SysResourceDTO> sysResourceDTOList = sysResourceService.searchSysResource(obj);
           result.setStatus(Status.SUCCESS);
           result.setMessage("查询成功!");
           result.setData(sysResourceDTOList);
           return result;
       }catch(Exception e) {
           log.error(e.getMessage(),e);
           result.setStatus(Status.FAILD);
           result.setMessage("执行异常,请重试");
           return result;
       }
   }

   @ResponseBody
   @RequestMapping(value="/queryLike",method = RequestMethod.POST)
   public ResponseResult<SysResourceDTO> queryLike(@RequestBody SysResourceDTO dto) throws Exception {
       ResponseResult<SysResourceDTO> result = new ResponseResult<>();
       try{
           SysResourceDTO sysResourceDTO = sysResourceService.queryLikeSysResource(dto);
           result.setStatus(Status.SUCCESS);
           result.setData(sysResourceDTO);
           result.setMessage("查询成功");
           return result;
       }catch(Exception e ) {
            log.error(e.getMessage(),e);
            result.setStatus(Status.FAILD);
            result.setMessage("执行异常,请重试");
            return result;
       }
   }

    /**
     * 新增数据
     * @param obj
     * @return
     * @throws Exception
     */
   @ResponseBody
   @RequestMapping(value="/create/v1",method = RequestMethod.POST)
   public ResponseResult create(SysResourceDTO obj) throws Exception {
        ResponseResult result = new ResponseResult();
        try{
            sysResourceService.insertSysResource(obj);
            result.setStatus(Status.SUCCESS);
            result.setMessage("添加成功");
            return result;
        }catch(Exception e) {
            log.error(e.getMessage(),e);
            result.setMessage("执行异常,请重试");
            result.setStatus(Status.FAILD);
            return result;
        }

   }

    /**
     * 更新数据
     * @param obj
     * @return
     * @throws Exception
     */
   @ResponseBody
   @RequestMapping(value="/update/v1",method = RequestMethod.POST)
   public ResponseResult update(@RequestBody SysResourceDTO obj) throws Exception {
       ResponseResult result = new ResponseResult();
       try{
           sysResourceService.updateSysResource(obj);
           result.setStatus(Status.SUCCESS);
           result.setMessage("修改成功");
           return result;
       }catch(Exception e) {
            result.setStatus(Status.FAILD);
            result.setMessage("执行异常,请重试");
            return result;
       }
   }

    /**
     * 根据id删除对象
     * @param id
     * @return
     * @throws Exception
     */
   @ResponseBody
   @RequestMapping(value="/delete/v1",method = RequestMethod.POST)
   public ResponseResult deleteResourceById(@RequestParam("id") String id) throws Exception {
       ResponseResult result = new ResponseResult();
       try{
           sysResourceService.deleteSysResourceById(id);
           result.setStatus(Status.SUCCESS);
           result.setMessage("删除成功");
           return result;
       }catch(Exception e) {
            log.error(e.getMessage(),e);
            result.setStatus(Status.FAILD);
            result.setMessage("执行异常,请重试");
            return result;
       }


   }
    /**
     * 根据userid查询shiro的权限
     * @param rolePermissionParam
     * @return
     */
    @RequestMapping(value="selectShiroUrlPermissionByUserId",method = RequestMethod.POST )
    public ResponseResult<RolePermission> selectShiroUrlPermissionByUserId(@RequestBody RolePermissionParam rolePermissionParam) {
        ResponseResult<RolePermission> responseResult = new ResponseResult<>();
        List<RolePermission> rolePermissionList = new ArrayList<>();

        try{
            SysResourceDTO sysResourceDTO = new SysResourceDTO();
            sysResourceDTO.setUserId(rolePermissionParam.getUserId());
            sysResourceDTO.setResourceType(rolePermissionParam.getResourceType());
            List<SysResourceDTO> sysResourceDTOList = sysResourceService.selectShiroUrlPermissionByUserId(sysResourceDTO);
            rolePermissionList = mapperUtil.map(sysResourceDTOList,RolePermission.class);
            responseResult.setStatus(Status.SUCCESS);
            responseResult.setDataList(rolePermissionList);
            responseResult.setMessage("查询成功");
            return responseResult;
        }catch(Exception e) {
            log.error(e.getMessage());
            responseResult.setStatus(Status.FAILD);
            responseResult.setMessage("执行异常,请重试!");
            return responseResult;
        }

    }
}
